/**
 * 音乐相关控制逻辑
 */
const path = require('path')
const express = require('express')
const formidable = require('formidable')

const fs = require('../../libs/fs')
const Music = require('../../models/music')
const Artist = require('../../models/artist')
const Album = require('../../models/album')

const router = module.exports = express.Router()

router.prefix = '/admin/music'

/**
 * GET /music
 */
router.get('/', async(req, res) => {

    const list = await Music.findAll({
        include: [{ model: Artist, as: 'artist' }, { model: Album, as: 'album' }]
    })

    res.locals.title = '音乐列表'
    res.locals.list = list
    res.render('admin/music/list')

})

/**
 * GET /music/add
 */
router.get('/add', async(req, res) => {

    const artists = await Artist.findAll()
    const albums = await Album.findAll()
    res.locals.title = '添加新音乐'
    res.locals.artists = artists
    res.locals.albums = albums
    res.render('admin/music/add')

})

/**
 * POST /music/add
 */
router.post('/add', async(req, res) => {
    const form = new formidable.IncomingForm()
    let upload_dir = path.join(global.UPLOAD_PATH, 'music');
    fs.mkdirsSync(upload_dir)
    form.uploadDir = upload_dir
    form.keepExtensions = true
    form.parse(req, (error, fields, files) => {
        if (error) throw error
            // 接收到客户端提交过来文件和填写的信息

        let music_url = fs.getUrl(path.basename(files.music.path), 'music')
        let poster_url = fs.getUrl(path.basename(files.poster.path), 'music')
        let lyric_url = fs.getUrl(path.basename(files.lyric.path), 'music')

        Music.create({
            name: fields.name,
            artist: fields.artist,
            duration: fields.duration,
            music: music_url,
            poster: poster_url,
            lyric: lyric_url,
            playCount: fields.playCount,
            albumId: fields.albumId,
            artistId: fields.artistId,
        })

        res.redirect('/admin/music')

    })


})

/**
 * GET /music/edit/:id
 */
router.get('/edit/:id', async(req, res) => {
    res.locals.title = '编辑'
        // 接收传过来的ID
    const id = parseInt(req.params.id || 0)
    if (!id) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }
    // 找到数组中的这个元素删除
    const music = await Music.findByPk(id)
    if (!music) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }

    const artists = await Artist.findAll()
    const albums = await Album.findAll()

    res.locals.title = '添加新音乐'
    res.locals.artists = artists
    res.locals.albums = albums
    res.locals.item = music
    res.render('admin/music/edit')

})

/**
 * POST /music/edit/:id
 */
router.post('/edit/:id', async(req, res) => {
    res.locals.title = '编辑'
        // 接收传过来的ID
    const id = parseInt(req.params.id || 0)
    if (!id) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }
    // 找到数组中的这个元素删除
    const music = await Music.findByPk(id)
    if (!music) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }


    const form = new formidable.IncomingForm()
    let upload_dir = path.join(global.UPLOAD_PATH, 'music');
    fs.mkdirsSync(upload_dir)
    form.uploadDir = upload_dir
    form.keepExtensions = true
    form.parse(req, (error, fields, files) => {
        if (error) throw error

        // 接收到客户端提交过来文件和填写的信息
        let music_url = fs.getUrl(path.basename(files.music.path), 'music')
        let poster_url = fs.getUrl(path.basename(files.poster.path), 'music')
        let lyric_url = fs.getUrl(path.basename(files.lyric.path), 'music')

        music.update({
            name: fields.name,
            artist: fields.artist,
            duration: fields.duration,
            music: music_url,
            poster: poster_url,
            lyric: lyric_url,
            playCount: fields.playCount,
            albumId: fields.albumId,
            artistId: fields.artistId,
        })

        res.redirect('/admin/music')
    })


})

/**
 * GET /music/delete/:id
 */
router.get('/delete/:id', async(req, res) => {
    // 接收传过来的ID
    const id = parseInt(req.params.id || 0)
    if (!id) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }
    // 找到数组中的这个元素删除
    Music.destroy({
        where: {
            id: id
        }
    }).then(() => {
        res.redirect('/admin/music')
    })



})